<?php

//小站控制器
class Site extends Controller {

	var $userid;
	var $userinfo;
	var $site;
	var $owner;
	var $opid;
	
	function Site()
	{
		parent::Controller();	
		$this->load->model("accounts_model", "am");
		$this->load->model("site_model", "sm");
		$this->load->model("classes_model", "cm");
		$this->load->model("options_model", "om");
		$this->load->model("plugins_model", "pm");
		
		//检测登陆状态
		$this->userid = $this->am->is_logined();
		if ($this->userid) {
			$this->userinfo = $this->am->get($this->userid);
		}

		//检测小站是否存在
		if((!$domain = u(2)) || (!$result = $this->sm->check_domainsite($domain))) {
			redirect("/");
		}
		 
		//获取小站信息
		$this->site = $this->sm->get_domainsite($domain);
		
		//获取站长信息
		$this->owner = $this->am->get($this->site["userid"]);
		
		//检测主人
		$this->isowner = $this->is_owner();
		//默认选项ID
		$this->opid = 0;
		
		//检测是否已关注
		$this->load->model("sitestatis_model", "ssm");
		$this->is_fansed = $this->ssm->is_fansed($this->site["id"], $this->userid);
		
		//设置此页面的过期 
		header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");      
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");    
		header("Cache-Control: no-cache, must-revalidate");   
		header("Pragma: no-cache");  
	}
	
	//小站默认首页
	function index()
	{
		//获取参数
		$action = u(3);
		
		//输出小站header
		if($action != "right" && $action != "show" && !($action == "forum" && u(4) == "show")) $this->site_header();
		
		$viewdata["site"] = $this->site;
		$viewdata["userid"] = $this->userid;
		$viewdata["is_fansed"] = $this->is_fansed;
		$viewdata["isowner"] = $this->isowner;
		
		if(!$action) {//首页
			$this->options(true);//默认第一个选项页面
		} else {
			//小站相关功能入口
			switch($action) {
				case "setting"://小站设置
					$this->setting();
				break;
				case "member"://关注人数
					$this->member();
				break;
				case "visits"://今日访客
					$this->visits();
				break;
				case "ad"://本站数据
					$this->ad();
				break;
				case "statis"://关注人数
					$this->statis();
				break;
				case "options"://小站选项(页面)
					$this->options();
				break;
				case "plugin"://小站选项应用
					$this->plugin();
				break;
				case "show"://小站选项应用内容
					$this->show();
				break;
				case "social"://小站社交活动
					$this->social();
				break;
				case "vote"://小站投票
					$this->vote();
				break;
				case "forum"://小站社区
					$this->forum();
				break;
				case "miniblog"://小站广播
					$this->miniblog();
				break;
				case "right"://小站右边部分
					$this->site_right();
				break;
			}
		}
		
		//输出公共footer
		if($action != "right") $this->autoload_model->output_footer();
	}
	
	//申请开站介绍
	function open()
	{
		//检测登陆状态
		if(!$this->userid) {
			show_failed("请先登陆", "accounts/");
		}
		//输出公共header
		$this->autoload_model->output_header();
		
		$viewdata["account"] = $this->account;
		$this->load->view(__CLASS__."/open", $viewdata);
		
		//输出公共footer
		$this->autoload_model->output_footer();
		
	}
	
	//查看右边部分内容
	function site_right()
	{
		ob_start();
		$viewdata["site"] = $this->site;
		$viewdata["cursite"] = $this->site;
		$viewdata["userid"] = $this->userid;
		$viewdata["is_fansed"] = $this->is_fansed;
		$viewdata["isowner"] = $this->isowner;
		
		//登陆状态检测
		$account = $this->am->get_account();
		$viewdata["account"] = $account;
		//关注的人也喜欢
		$fansites = $this->sm->fans_site();
		$viewdata["fansites"] = $fansites;
		//关注人数与今日访客
		$myfuns = $this->ssm->get_sitefuns($this->site["id"]);
		$myvisits = $this->ssm->get_visits($this->site["id"]);
		$viewdata["myfuns"] = $myfuns;
		$viewdata["myvisits"] = $myvisits;
		
		//获取广告内容
		$ad28 = $this->get_site_ad(28);
		$ad29 = $this->get_site_ad(29);
		$ad30 = $this->get_site_ad(30);
		$ad36 = $this->get_site_ad(36);
		$ad37 = $this->get_site_ad(37);
		$ad38 = $this->get_site_ad(38);
		$viewdata['ad28'] = $ad28;
		$viewdata['ad29'] = $ad29;
		$viewdata['ad30'] = $ad30;
		$viewdata['ad36'] = $ad36;
		$viewdata['ad37'] = $ad37;
		$viewdata['ad38'] = $ad38;
		
		$this->load->view(__CLASS__.'/site_right', $viewdata);
		$contents = jsoutputformat(ob_get_contents());//js输出转换
		ob_end_clean();
		echo "document.write('$contents');";exit;
	}
	
	//获取广告内容
	function get_site_ad($placeid)
	{
		$adstring1 = "";
		$this->load->model("ads_model");
		$ad1 = $this->ads_model->get($placeid, $this->site['id']);
		if (is_array($ad1) && count($ad1)>0) {
			//更新views
			$this->ads_model->setviews($ad1['adsid']);
			if($ad1['linkurl'] == "" || $ad1['linkurl'] == "http://") {//可支持不加链接
				$adstring1 = "<img width='".$ad1['place']['width']."' height='".$ad1['place']['height']."' src='".gurl("data/upload_pic").'/'.$ad1['imageurl']."'>";
			} else {
				$adstring1 = "<a href='".$this->setlink($ad1['adsid'], $ad1['linkurl'])."' target='_blank'><img width='".$ad1['place']['width']."' height='".$ad1['place']['height']."' src='".gurl("data/upload_pic").'/'.$ad1['imageurl']."'></a>";
			}
			if(isset($ad1['place']) && $ad1['place']['height'] > 0) {
				$adstring1 = '<div class="ad'.$ad1['place']['height'].'">'.$adstring1.'</div>'; 
			}
		}
		return $adstring1;
	}
	
	//设置广告点击链接
	function setlink($adsid, $linkurl)
	{
		$linkurl = gurl('/ads/link/?url='.$linkurl.'&adsid='.$adsid);
		return $linkurl;
	}
	
	//查看应用内容
	function show()
	{
		if(!$modid = u(4) || !$id = u(5)) {
			show_failed("非法请求");
		}
		$modid = u(4);
		$id = u(5);
		$offset = u(6);
		$module = $this->pm->get_module($modid);
		if(!isset($module) || count($module) == 0) show_404();
		$modulename = $module["modulename"];
		$viewpath = __CLASS__.'/'.$modulename;
		$plugmodel = $modulename."_model";
		$this->load->model($plugmodel);//加载
		
		$viewdata = $this->$plugmodel->show($id);
		if(!$viewdata || count($viewdata) == 0) show_404();
		$viewdata['offset'] = $offset;//分页处理
		$viewdata['moduleid'] = $modid;
		
		//选项数据
		$viewdata['currentplugin'] = $this->pm->get($viewdata['plid']);
		
		//输出header+标题
		$title = "";
		if($modid == 8) {
			if($viewdata['content'] != "") $title = $viewdata['content'].'_';
		} else {
			if($viewdata['title'] != "") $title = $viewdata['title'].'_';
		}
		$pagedata["title"] = $title.$this->site['name']." | 热闹网";
		$this->site_header($pagedata);
		$this->load->view($viewpath.'/show', $viewdata);
	}
	
	//查看社交活动参与名单
	function social()
	{
		if(!$id = u(4)) {
			show_failed("非法请求");
		}
		$offset = u(5);
		$per_page = 80;
		$this->load->model("social_model");//加载
		$lists = $this->social_model->member($id, $per_page, $offset);
		
		//分页处理
		$base_url = c('site_url').$this->site['domain'].'/social/'.$id;
		$total_rows = $this->social_model->count_member($id);
		$pagestring = pages($base_url,$total_rows,5,$per_page);
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		
		$viewdata['lists'] = $lists;
		$viewdata['offset'] = $offset;//分页处理
		$this->load->view(__CLASS__.'/social/member', $viewdata);
	}
	
	//投票
	function vote()
	{
		if ($dosubmit = p("dosubmit")) {
			$this->load->model("vote_model");//加载
			$dataid = p("dataid");
			$moduleid = p("moduleid");
			$result = $this->vote_model->post($dataid);
			show_success("投票成功", '/site/'.$this->site['domain'].'/show/'.$moduleid.'/'.$dataid);
		}
	}
	
	//小站选项应用
	function plugin()
	{
		if(!$action = u(4) || !$plid = u(5)) {
			show_failed("非法请求");
		}
		//加载对应应用模块
		$this->plid = u(5);
		$this->action = u(4);
		$plugin = $this->pm->get($this->plid);
		if(!isset($plugin) || count($plugin) == 0) show_failed("非法请求");
		$module = $this->pm->get_module($plugin["moduleid"]);
		if(!isset($module) || count($module) == 0) show_failed("非法请求");
		$this->moduleid = $plugin["moduleid"];
		$this->opid = $plugin["opid"];
		$modulename = $module["modulename"];
		$plugmodel = $modulename."_model";
		$this->load->model($plugmodel);//加载
		
		$this->viewpath = __CLASS__.'/'.$modulename;
		$this->viewdata = array();
		$this->viewdata["plid"] = $this->plid;
		
		//模块操作
		switch($this->action) {
			case "list"://应用列表页
				$this->__plugin_list($plugmodel);
			break;
			case "add"://添加
				$this->__plugin_add($plugmodel);
			break;
			case "edit"://编辑
				$this->dataid = u(6);//数据ID
				$this->__plugin_edit($plugmodel);
			break;
			case "delete"://删除
				$this->dataid = u(6);//数据ID
				$this->__plugin_delete($plugmodel);
			break;
			case "preview"://预览
				$this->__plugin_preivew($plugmodel);
			break;
		}
	}
	
	//plugin 内容添加
	function __plugin_add($plugmodel)
	{
		//添加权限审核：站长或已关注成员
		if(!$this->isowner && !$this->is_fansed) {
			show_failed("非法请求");
		}
		if(!$this->userid) show_failed("请先登陆", "accounts/");
		if($dosubmit = p("dosubmit")) {
			$result = $this->$plugmodel->add($this->plid);//执行添加
			if(!$result) {//添加失败: 热点图片限制审核不过
				if(isset($_POST))  $this->viewdata = array_merge($_POST, $this->viewdata);
				$this->load->view($this->viewpath.'/add_error', $this->viewdata);
				return;
			}
			//...
			if($this->moduleid == 3) {//公告栏直接进入选项页
				show_success("添加成功", '/site/'.$this->site['domain'].'/options/'.$this->opid);
			} else {
				//添加新鲜事
				$public = ($this->userid != $this->site['userid']) ? 1 : 0;
				$data = array(
					'title' => p("title"),
					'userid' => $this->userid,
					'username' => $this->userinfo['username'],
					'userphoto' => $this->userinfo['photo'],
					'siteid' => $this->site['id'],
					'sitename' => $this->site['name'],
					'sitelogo' => $this->site['info']['logo'],
					'action' => 'add',
					'link' => '/site/'.$this->site['domain'].'/show/'.$this->moduleid.'/'.$result,
					'addtime' => time(),
					'public' => $public,
				);
				$this->autoload_model->add_fresh($this->moduleid, $data);
				show_success("添加成功", '/site/'.$this->site['domain'].'/show/'.$this->moduleid.'/'.$result);
			}
		}
		if($this->moduleid == 3) {//公告栏获取当前内容再进入页面
			$this->viewdata["lastdata"] = $this->$plugmodel->get($this->plid);
		}
		$this->viewdata["plugininfo"] = $this->pm->get($this->plid);
		$this->load->view($this->viewpath.'/add', $this->viewdata);
	}
	
	//plugin 内容编辑
	function __plugin_edit($plugmodel)
	{
		if(!$this->userid) show_failed("请先登陆", "accounts/");
		if($dosubmit = p("dosubmit")) {
			$result = $this->$plugmodel->edit($this->plid);//执行编辑
			if(!$result) {//添加失败: 热点图片限制审核不过
				if(isset($_POST))  $this->viewdata = array_merge($_POST, $this->viewdata);
				$this->load->view($this->viewpath.'/edit_error', $this->viewdata);
				return;
			}
			//...
			if(strpos($this->dataid, 'ti_')){//批量操作 - 跳转到列表
				show_success("编辑成功", '/site/'.$this->site['domain'].'/plugin/list/'.$this->plid);
			} else {
				show_success("编辑成功", '/site/'.$this->site['domain'].'/show/'.$this->moduleid.'/'.$this->dataid);
			}
		}
		//获取模型编辑数据
		$editdata = $this->$plugmodel->get_info($this->dataid);
		if (!is_array($editdata) || count($editdata) == 0) {
			show_failed("非法请求");
		}
		
		//编辑权限审核：作者本身
		if($this->moduleid != 8) {//相册除外
			if($editdata["userid"] != $this->userid) {
				show_failed("非法请求");
			}
		}
		$this->viewdata["editdata"] = $editdata;
		$this->load->view($this->viewpath.'/edit', $this->viewdata);
	}
	
	//plugin 内容删除
	function __plugin_delete($plugmodel)
	{
		if(!$this->userid) show_failed("请先登陆", "accounts/");
		//删除权限审核：作者本身或站长
		//获取模型数据
		$editdata = $this->$plugmodel->get_info($this->dataid);
		if($this->moduleid == 8) {//相册除外
			$editdata = $editdata[0];
		}
		if(!$this->isowner && ($editdata["userid"] != $this->userid)) {
			show_failed("非法请求");
		}
		if($this->moduleid == 8) {//相册除外
			$result = $this->$plugmodel->deleteinfo($this->dataid);//执行编辑
		} else {
			$result = $this->$plugmodel->delete($this->dataid);//执行编辑
		}
		//...
		show_success("删除成功", '/site/'.$this->site['domain'].'/options/'.$this->opid);
	}
	
	//plugin 内容预览
	function __plugin_preivew($plugmodel)
	{
		$viewdata = $this->$plugmodel->preivew();
		if(!$viewdata || count($viewdata) == 0) show_failed("非法请求");
		$this->viewdata = array_merge($viewdata, $this->viewdata);
		$this->load->view($this->viewpath.'/preview', $this->viewdata);
	}
	
	//查看应用列表
	function __plugin_list($plugmodel)
	{
		$offset = u(6);
		$per_page = 20;
		if ($this->moduleid == 7) {
			$per_page = 30;
		}elseif ($this->moduleid == 8) {
			$per_page = 50;
		} 
		$lists = $this->$plugmodel->lists($this->plid, $per_page, $offset);
		//分页处理
		$base_url = c('site_url').$this->site['domain'].'/plugin/list/'.$this->plid;
		$total_rows = $this->$plugmodel->count_lists($this->plid);
		$pagestring = pages($base_url,$total_rows,6,$per_page);
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		
		//选项数据
		$currentplugin = $this->pm->get($this->plid);
		
		$viewdata['lists'] = $lists;
		$viewdata["is_fansed"] = $this->is_fansed;
		$viewdata['currentplugin'] = $currentplugin;
		if(!$viewdata || count($viewdata) == 0) show_failed("非法请求");
		$this->viewdata = array_merge($viewdata, $this->viewdata);
		$this->load->view($this->viewpath.'/lists', $this->viewdata);
	}
	
	//小站社区
	function forum()
	{
		$this->load->model("forum_model", "fm");
		$action = u(4);
		$offset = u(6);
		$per_page = 50;
		//小站选项数据
		$options = $this->om->get_siteoptions($this->site["id"]);

		$this->viewdata["site"] = $this->site;
		$this->viewdata["userid"] = $this->userid;
		$this->viewdata["is_fansed"] = $this->is_fansed;
		$this->viewdata["isowner"] = $this->isowner;
		$this->viewdata["options"] = $options;
		$this->viewdata["offset"] = $offset;
		
		switch($action) {
			case "add"://发表话题
				$this->__forum_add();
			break;
			case "preview"://预览话题
				$this->__forum_preview();
			break;
			case "show"://查看话题
				$this->_forum_show();
			break;
			case "delete"://删除话题
				$this->_forum_delete();
			break;
			case "edit"://编辑话题
				$this->_forum_edit();
			break;
			default:
				$sortid = u(5);
				if(!$sortid) $sortid = 1;
				$offset = u(6);
				$lists = $this->fm->lists($this->site["id"], $per_page, $offset, $sortid);
				//分页处理
				$base_url = c('site_url').$this->site['domain'].'/forum/'.$this->site["id"].'/'.$sortid;
				$total_rows = $this->fm->count_lists($this->site["id"]);
				$pagestring = pages($base_url,$total_rows,6,$per_page);
				$this->viewdata["offset"] = $offset;
				$this->viewdata["pagestring"] = $pagestring;
				$this->viewdata["lists"] = $lists;
				$this->viewdata["sortid"] = $sortid;
				$this->load->view(__CLASS__.'/forum', $this->viewdata);
		}
	}
	
	//编辑话题
	function _forum_edit()
	{
		if(!$id = u(5)) {
			show_failed("非法请求");
		}
		if($dosubmit = p("dosubmit")) {
			(!p("iswall")) ? $iswall = 0 : $iswall = 1; 
			$id = p("id");
			$data = array(
				"siteid" => $this->site["id"],
				"classid" => $this->cm->get_topclassid($this->site["classid"]),
				"title" => p("title"),
				"description" => p("description"),
				"iswall" => $iswall,
				"userid" => $this->userid,
			);
			$result = $this->fm->edit($data, $id);//执行编辑
			//...
			if($result) {
				show_success("编辑成功", "site/".$this->site["domain"]."/forum/show/".$id);
			} else {
				show_failed("编辑失败");
			}
		}		
		$row = $this->fm->show($id);
		$this->viewdata["row"] = $row;
		$this->load->view(__CLASS__.'/forum/edit', $this->viewdata);
	}
	
	//删除话题
	function _forum_delete()
	{
		if(!$id = u(5)) {
			show_failed("非法请求");
		}
		$result = $this->fm->delete($id);
		if($result) show_success("删除成功", "/site/".$this->site['domain']."/forum");
	}
	
	//查看话题
	function _forum_show()
	{
		if(!$id = u(5)) {
			show_failed("非法请求");
		}
		$row = $this->fm->show($id);
		if(!$row || count($row) == 0) show_failed("非法请求");
		
		//赞美数据
		$this->load->model("digg_model");
		$digg = $this->digg_model->get($row["userid"]);
		$diggcount = $this->digg_model->get_count($row["userid"]);
		$isdigg = $this->digg_model->checked($this->userid, $row["id"]);
		
		//关注的人也喜欢
		$fansites = $this->sm->fans_site();
		$this->viewdata["fansites"] = $fansites;
		$this->viewdata["row"] = $row;
		$this->viewdata["digg"] = $digg;
		$this->viewdata["diggcount"] = $diggcount;
		$this->viewdata["isdigg"] = $isdigg;
		
		//输出header+标题
		$pagedata["title"] = $row['title'].'_'.$this->site['name']." | 热闹网";
		$this->site_header($pagedata);
		
		$this->load->view(__CLASS__.'/forum/show', $this->viewdata);
	}
	
	//预览话题
	function __forum_preview()
	{
		$viewdata = $this->fm->preivew();
		if(!$viewdata || count($viewdata) == 0) show_failed("非法请求");
		$this->viewdata = array_merge($viewdata, $this->viewdata);
		$this->load->view(__CLASS__.'/forum/preview', $this->viewdata);
	}
	
	//发表话题
	function __forum_add()
	{
		//添加权限审核：站长或已关注成员
		//...
		if(!$this->userid) show_failed("请先登陆", "accounts/");
		
		if($dosubmit = p("dosubmit")) {
			(!p("iswall")) ? $iswall = 0 : $iswall = 1; 
			$data = array(
				"siteid" => $this->site["id"],
				"classid" => $this->cm->get_topclassid($this->site["classid"]),
				"title" => p("title"),
				"description" => p("description"),
				"iswall" => $iswall,
				"userid" => $this->userid,
			);
			if($id = p("id")) {//编辑
				$result = $this->fm->edit($data, $id);//执行编辑
				//...
				if($result) {
					show_success("编辑成功", "site/".$this->site["domain"]."/forum/show/".$id);
				} else {
					show_failed("编辑失败");
				}
			} else {//添加
				$result = $this->fm->add($data);//执行添加
				//...
				if($result) {
					
					//添加新鲜事
					/*$data = array(
						'title' => p("title"),
						'userid' => $this->userid,
						'username' => $this->userinfo['username'],
						'userphoto' => $this->userinfo['photo'],
						'siteid' => $this->site['id'],
						'sitename' => $this->site['name'],
						'sitelogo' => $this->site['info']['logo'],
						'action' => 'add',
						'link' => '/site/'.$this->site['domain'].'/forum/show/'.$result,
						'addtime' => time(),
					);
					$this->autoload_model->add_fresh(9, $data);*/
					
					show_success("发表成功", "site/".$this->site["domain"]."/forum");
				} else {
					show_failed("发表失败");
				}
			}
		}
		$this->load->view(__CLASS__.'/forum/add', $this->viewdata);
	}
	
	//小站广播
	function miniblog()
	{
		$this->load->model("miniblog_model", "mbm");
		//小站选项数据
		$options = $this->om->get_siteoptions($this->site["id"]);

		$offset = u(4);
		$this->viewdata["site"] = $this->site;
		$this->viewdata["userid"] = $this->userid;
		$this->viewdata["is_fansed"] = $this->is_fansed;
		$this->viewdata["isowner"] = $this->isowner;
		$this->viewdata["options"] = $options;
		$this->viewdata["offset"] = $offset;
		
		if($dosubmit = p("dosubmit")) {
			$data = array(
				"description" => p("description"),
				"siteid" => $this->site["id"],
				"classid" => $this->cm->get_topclassid($this->site["classid"]),
				"userid" => $this->userid,
			);
			$result = $this->mbm->add($data);//执行添加
			//...
			if($result) {
				//添加新鲜事
				$data = array(
				'title' => p("description"),
				'userid' => $this->userid,
				'username' => $this->userinfo['username'],
				'userphoto' => $this->userinfo['photo'],
				'siteid' => $this->site['id'],
				'sitename' => $this->site['name'],
				'sitelogo' => $this->site['info']['logo'],
				'action' => 'add',
				'link' => '/site/'.$this->site['domain'].'/miniblog',
				'addtime' => time(),
				);
				$this->autoload_model->add_fresh(10, $data);
				show_success("发表成功", "site/".$this->site["domain"]."/miniblog");
			} else {
				show_failed("发表失败");
			}
		}
		
		//获取广播信息
		$row = $this->mbm->get($this->site["id"]);
		$this->viewdata["row"] = $row;
		$this->load->view(__CLASS__.'/miniblog', $this->viewdata);
	}
	
	//小站选项(页面)
	//http://www.renao100.com/site/fengbeijing/options/1
	function options($default = false)
	{
		if (!$default && !$opid = u(4)) {
			show_failed("非法请求");
		}
		
		//小站选项数据
		$options = $this->om->get_siteoptions($this->site["id"]);
		if($default) {//默认首页
			if (isset($options[0]['id']) && $options[0]['id']>0) {//有选项
				$opid = $options[0]['id'];
			} else {//无选项默认进入社区:如默认选项一则需考虑选项删除后可能不存在选项情况
				$this->forum();
				return ;
			}
		}
		
		//获取选项
		$currentoption = $this->om->get($opid);
		if(count($currentoption) == 0) show_failed("非法请求");
		
		//页面模块列表
		$modules = $this->pm->get_modules();
		
		//选项应用列表数据
		$oplugins = $this->pm->get_oplugins($opid);
		//应用列表数据(各个模块)
		foreach($oplugins as $k => $plugin) {
			$plid = $plugin["id"];
			$module = $this->pm->get_module($plugin["moduleid"]);
			$modulename = $module["modulename"];
			$plugmodel = $modulename."_model";
			$this->load->model($plugmodel);//加载
			//应用数据列表
			$oplugins[$k]["lists"] = $this->$plugmodel->lists($plid);
		}
		
		$viewdata["site"] = $this->site;
		$viewdata["userid"] = $this->userid;
		$viewdata["is_fansed"] = $this->is_fansed;
		$viewdata["isowner"] = $this->isowner;
		$viewdata["options"] = $options;
		$viewdata["currentoption"] = $currentoption;
		$viewdata["opid"] = $opid;
		$viewdata["modules"] = $modules;
		$viewdata["oplugins"] = $oplugins;
		$this->load->view(__CLASS__.'/options', $viewdata);
	}
	
	//关注人数
	function member()
	{
		$this->load->model("sitestatis_model", "ssm");
		$funs = $this->ssm->get_funs($this->site["id"]);
		$viewdata["funs"] = $funs;
		$viewdata["site"] = $this->site;
		$this->load->view(__CLASS__.'/member', $viewdata);
	}
	
	//今日访客
	function visits()
	{
		$offset = u(4);
		$per_page = 80;
		$this->load->model("sitestatis_model", "ssm");
		$visits = $this->ssm->get_todayvisits($this->site["id"], $per_page, $offset);
		//分页处理
		$base_url = c('site_url').$this->site['domain'].'/visits';
		$total_rows = $this->ssm->get_visits($this->site["id"]);
		$pagestring = pages($base_url,$total_rows,4,$per_page);
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		$viewdata["visits"] = $visits;
		$viewdata["site"] = $this->site;
		$this->load->view(__CLASS__.'/visits', $viewdata);
	}
	
	//本站数据
	function ad()
	{
		$this->load->model("sitestatis_model", "ssm");
		
		//本周新增粉丝数据统计
		$new_weekfuns = $this->ssm->statis_newfuns($this->site["id"]);
		//本月新增粉丝数据统计
		$new_monthfuns = $this->ssm->statis_newfuns($this->site["id"], "month");
		//小站统计数据
		$sitestatis = $this->ssm->get_sitestatis($this->site["id"]);
		
		$avg_fanstaytime = $avg_visitstaytime = $visits2fans = $avg_visits = 0;
		if(count($sitestatis)>0) {
			//每位粉丝平均停留时间
			if($sitestatis["fans"] > 0) $avg_fanstaytime = ceil($sitestatis["fanstaytime"]/$sitestatis["fans"]);
			if($sitestatis["visits"] > 0) {
				//每位访客平均停留时间
//				$avg_visitstaytime = ceil($sitestatis["visitstaytime"]/$sitestatis["visits"]);
				//访客转为粉丝的比率
//				$visits2fans = round($sitestatis["fans"]/$sitestatis["visits"], 2)*100;
				//每日访客
				$days = time() - $this->site["regtime"]/86400;
				$avg_visits = ceil($sitestatis["visits"]/$days);
			}
		} 
		$viewdata["site"] = $this->site;
		$viewdata["new_weekfuns"] = $new_weekfuns;
		$viewdata["new_monthfuns"] = $new_monthfuns;
		$viewdata["avg_fanstaytime"] = $avg_fanstaytime;
		$viewdata["avg_visitstaytime"] = $avg_visitstaytime;
		$viewdata["visits2fans"] = $visits2fans;
		$viewdata["avg_visits"] = $avg_visits;
		$this->load->view(__CLASS__.'/data', $viewdata);
	}
	
	//关注人数
	function statis()
	{
		$offset = u(4);
		$per_page = 80;
		$this->load->model("sitestatis_model", "ssm");
		$fans = $this->ssm->get_funs($this->site["id"], $per_page, $offset);
		//分页处理
		$base_url = c('site_url').$this->site['domain'].'/statis';
		$total_rows = $this->ssm->get_sitefuns($this->site["id"]);
		$pagestring = pages($base_url,$total_rows,4,$per_page);
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		$viewdata["fans"] = $fans;
		$viewdata["site"] = $this->site;
		$this->load->view(__CLASS__.'/statis', $viewdata);
	}
	
	//小站设置
	function setting()
	{
		//检测主人
		if(!$this->isowner) show_success("非法请求", "/");
		
		//小站管理菜单二级
		if(!$action = u(4)) {//小站信息
			$this->__setting();
		} else {
			switch($action) {
				case "skin"://风格设计
					$this->__setting_skin();
				break;
				case "member"://成员管理
					$this->__setting_member();
				break;
				case "ads"://广告管理
					$this->__setting_ads();
				break;
			}
		}
	}
	
	//设置基本信息
	function __setting()
	{
		$this->load->model("tag_model");
		//================= LOGO图片处理 ============/
		//加载类库
		$this->load->library("jqphoto");
		//1) 表单1：大图片上传处理
		if($dosubmit = p("dosubmit")  && $_FILES['image']['name'] != "") {
			$this->jqphoto->upload($_FILES['image']);
		}
		//2) 表单2：裁切图处理
		if($dosubmit2 = p("dosubmit2")) {
			$this->jqphoto->upload_thumb($_POST);
		}
		//3) 获取数据
		$current_large_image_width = $current_large_image_height = "";
		if($this->jqphoto->large_photo_exists != "") {
			$current_large_image_width = $this->jqphoto->getWidth($this->jqphoto->large_image_location);
			$current_large_image_height = $this->jqphoto->getHeight($this->jqphoto->large_image_location);
		}
		$viewdata["large_photo_exists"] = $this->jqphoto->large_photo_exists;
		$viewdata["thumb_photo_exists"] = $this->jqphoto->thumb_photo_exists;
		$viewdata["upload_path"] = $this->jqphoto->upload_path;
		$viewdata["large_image_name"] = $this->jqphoto->large_image_name;
		$viewdata["thumb_width"] = $this->jqphoto->thumb_width;
		$viewdata["thumb_height"] = $this->jqphoto->thumb_height;
		$viewdata["current_large_image_width"] = $current_large_image_width;
		$viewdata["current_large_image_height"] = $current_large_image_height;
		//================= LOGO图片处理结束 ============/

		//好站分类
		$currentclass = $this->cm->get($this->site["classid"]);
		$topclass = $this->cm->get_subclass();
		$subclass = $this->cm->get_subclass($currentclass["parentid"]);
		
		//好站标签
		$tagslist = $this->tag_model->get_site($this->site["id"]);
		
		//标签信息列表
		$tags = $this->tag_model->lists();
		
		$viewdata["currentclass"] = $currentclass;
		$viewdata["topclass"] = $topclass;
		$viewdata["subclass"] = $subclass;
		$viewdata["tags"] = $tags;
		$viewdata["tagslist"] = $tagslist;
		
		$this->load->view(__CLASS__.'/setting', $viewdata);
	}
	
	//设置风格
	function __setting_skin()
	{
		$this->load->model("skins_model");
		//处理背景表单提交
		if($dosubmit = p("dosubmit")) {
			
			  $config['upload_path'] = './data/upload_pic/';
			  $config['allowed_types'] = 'gif|jpg|jpeg|png';
			  //$config['max_size'] = '2048';//k
			  $config['max_width']  = '0';
			  $config['max_height']  = '0';
			  $config['file_name']  = "background_".strtotime(date('Y-m-d H:i:s'));
			  $this->load->library('upload', $config);
			  if ( !$this->upload->do_upload("background")) {
				show_failed("上传失败");
			  } 
			  $upload_data = $this->upload->data();
			  
			 	//缩略图片
			    $config2['image_library'] = 'gd2';
				$config2['source_image'] = './data/upload_pic/'.$upload_data["file_name"];
//				$config2['create_thumb'] = TRUE;
				$config2['maintain_ratio'] = TRUE;
				$config2['master_dim'] = 'height';
				$config2['width'] = 150;
				$config2['height'] = 400;
				$this->load->library('image_lib', $config2); 
				$this->image_lib->resize();
			  
			  $data = array("background"=>$upload_data["file_name"]);
			  $result = $this->sm->update_siteinfo($this->site["id"], $data);
			  show_success("修改成功", '/site/'.$this->site['domain'].'/setting/skin');
		}
	
		//皮肤列表
		$skins = $this->skins_model->lists();
		$viewdata["skins"] = $skins;
		$this->load->view(__CLASS__.'/setting_skin', $viewdata);
	}
	
	//设置成员
	function __setting_member()
	{
		$this->load->model("sitestatis_model", "ssm");
		$pagestring = "";
		$fans = array();
		$offset = u(5);
		$per_page = 80;
		
		if ($dosubmit = p("dosubmit")) {//搜索用户
			$keyword = safeEncoding(trim(p("keyword")));
			if ($keyword != "") {
				$fans = $this->ssm->search_funs($this->site["id"], $keyword);
			}
		} else {
			$fans = $this->ssm->get_funs($this->site["id"], $per_page, $offset);
			//分页处理
			$base_url = c('site_url').$this->site['domain'].'/setting/member';
			$total_rows = $this->ssm->get_sitefuns($this->site["id"]);
			$pagestring = pages($base_url,$total_rows,5,$per_page);
			
		}
		$viewdata["offset"] = $offset;
		$viewdata["pagestring"] = $pagestring;
		$viewdata["fans"] = $fans;
		$viewdata["site"] = $this->site;
		$this->load->view(__CLASS__.'/setting_member', $viewdata);
	}
	
	//设置广告
	function __setting_ads()
	{
		$this->load->model("ads_model");
		if($dosubmit1 = p("dosubmit1")) {
			$data = array(
				'placeid' => 36,
				'adsname' => $this->site['name'].'广告1',
				'linkurl' => p('linkurl1'),
				'fromdate' => time(),
				'todate' => 0,
				'passed' => 1,
				'addtime' => time(),
				'siteid' => $this->site['id'],
			);
			$result = $this->ads_model->add($data, 'imageurl1');
			if($result) {//提示成功
				show_failed("添加成功", '/site/'.$this->site['domain'].'/setting/ads');
			} else {
				show_failed("添加失败");
			}
		}
		
		if($dosubmit2 = p("dosubmit2")) {
			$data = array(
				'placeid' => 37,
				'adsname' => $this->site['name'].'广告2',
				'linkurl' => p('linkurl2'),
				'fromdate' => time(),
				'todate' => 0,
				'passed' => 1,
				'addtime' => time(),
				'siteid' => $this->site['id'],
			);
			$result = $this->ads_model->add($data, 'imageurl2');
			if($result) {//提示成功
				show_failed("添加成功", '/site/'.$this->site['domain'].'/setting/ads');
			} else {
				show_failed("添加失败");
			}
		}
		$viewdata["site"] = $this->site;
		$this->load->view(__CLASS__.'/setting_ads', $viewdata);
	}
	
	//检测是否是小站主人
	function is_owner()
	{
		if($this->userid > 0 && $this->userid == $this->owner["id"]) return true;
		return false;
	}
	
	//小站Header输出
	function site_header($pagedata = array()) 
	{
		$this->load->model("skins_model");
		if(!isset($pagedata["title"])) $pagedata["title"] = $this->site['name']." | 热闹网";
		if(!isset($pagedata["keywords"])) $pagedata["keywords"] = "热闹、renao、热闹网、renao100、热闹100、轻站、轻、网站、轻型网站、维基、小站、兴趣、专页、博客、社区、空间、互动";
		if(!isset($pagedata["description"])) $pagedata["description"] = "热闹网(renao100.com) - 让每个人拥有史上最灵活的兴趣空间。为自己喜欢的事物建立轻型网站，并允许维基创建内容。";
		
		$viewdata["site"] = $this->site;
		$viewdata["owner"] = $this->owner;
		$viewdata["isowner"] = $this->isowner;
		
		//获取皮肤设置
		$siteskins = $this->skins_model->get_site($this->site["id"]);
		
		//登陆状态检测
		$account = $this->am->get_account();
		
		//好站分类
		$currentclass = $this->cm->get($this->site["classid"]);
		$parentclass = $this->cm->get($currentclass['parentid']);
		
		//选项数据
		$options = $this->om->get_siteoptions($this->site["id"]);
		//当前选项ID
		$action = "";
		if(u(4)) $this->opid = u(4);
		if(u(3)) $action = u(3);
		
		//处理列表选中
		$current_opid = 0;
		if ($action == "plugin") {
			if(u(4) == "list") {
				$current_plid = u(5);
				$plugin = $this->pm->get($current_plid);
				$current_opid = $plugin['opid'];
			} 
		} elseif ($action == 'show'){
			$modid = u(4);
			$id = u(5);
			$module = $this->pm->get_module($modid);
			if(!isset($module) || count($module) == 0) show_failed("非法请求");
			$modulename = $module["modulename"];
			$viewpath = __CLASS__.'/'.$modulename;
			$plugmodel = $modulename."_model";
			$this->load->model($plugmodel);//加载
			$showdata = $this->$plugmodel->show($id);
			if(isset($showdata['plid'])) {
				$plugin = $this->pm->get($showdata['plid']);
				$current_opid = $plugin['opid'];
			} else {
				show_404();
			}
		}
		$viewdata["pagedata"] = $pagedata;
		$viewdata["account"] = $account;
		$viewdata["options"] = $options;
		$viewdata["opid"] = $this->opid;
		$viewdata["action"] = $action;
		$viewdata["siteskins"] = $siteskins;
		$viewdata["current_opid"] = $current_opid;
		$viewdata["currentclass"] = $currentclass;
		$viewdata["parentclass"] = $parentclass;
		header("Content-Type:text/html;charset=utf-8");
		$this->load->view(__CLASS__.'/site_header', $viewdata);
	}
	
}

/* End of file */